import { NextRequest, NextResponse } from 'next/server';
import { startLiteMonitoring } from '@/lib/services-init';
import { createModuleLogger } from '@/utils/logger';

const logger = createModuleLogger('admin-monitoring-api');

export async function POST(request: NextRequest) {
  try {
    logger.info('🚀 启动链监控服务...');
    
    // 使用轻量级监控启动
    const result = await startLiteMonitoring();
    
    return NextResponse.json({
      success: result.success,
      message: result.message,
      status: result.status,
      activeCount: result.activeCount,
      servicesReady: true,
      startedAt: new Date().toISOString()
    });
    
  } catch (error) {
    logger.error('❌ 启动监控失败:', error);
    return NextResponse.json(
      { 
        success: false, 
        error: '启动监控失败',
        details: error instanceof Error ? error.message : String(error)
      },
      { status: 500 }
    );
  }
}

export async function GET(request: NextRequest) {
  try {
    const { getMonitoringManager } = await import('@/lib/services-init');
    const monitoringManager = getMonitoringManager();
    
    if (!monitoringManager) {
      return NextResponse.json(
        { success: false, error: '监控管理器未初始化' },
        { status: 500 }
      );
    }

    const status = monitoringManager.getAllStatus();
    
    return NextResponse.json({
      success: true,
      status,
      environment: process.env.NODE_ENV,
      autoStartEnabled: process.env.AUTO_START_MONITORING === 'true',
      timestamp: new Date().toISOString()
    });
    
  } catch (error) {
    logger.error('❌ 获取监控状态失败:', error);
    return NextResponse.json(
      { 
        success: false, 
        error: '获取监控状态失败',
        details: error instanceof Error ? error.message : String(error)
      },
      { status: 500 }
    );
  }
} 